package com.itheima.eurekaprovider.dao.impl;

import com.itheima.eurekaprovider.dao.CustomerDao;
import com.itheima.eurekaprovider.pojo.Customer;
import com.itheima.eurekaprovider.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository("customerDao")
public class CustomerDaoImpl implements CustomerDao {


    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public Customer findByUsername(String username) {
        String sql = "select * from user where username=?";
        RowMapper<Customer> rowMapper = new BeanPropertyRowMapper<>(Customer.class);
        Customer customer = null;
        try {
            customer = this.jdbcTemplate.queryForObject(sql, rowMapper, customer.getUsername());
        } catch (Exception e) {
            customer = null;
        }
        return customer;
    }

    @Override
    public List<Customer> findAll() {
        String sql = "select * from customer";
        RowMapper<Customer> rowMapper = new BeanPropertyRowMapper<>(Customer.class);
        List<Customer> customerList = this.jdbcTemplate.query(sql, rowMapper);
        return customerList;
    }
}
